import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    public static void main(String[] args) {
        int[][] isWater={{0,1},{0,0}};
        highestPeak(isWater);
        System.out.println(highestPeak(isWater));
    }
    static int[] dx={0,0,1,-1};
   static   int[] dy={1,-1,0,0};
    public static int[][] highestPeak(int[][] isWater) {
        int m=isWater.length;int n=isWater[0].length;
        int[][] dist=new int[m][n];
        for(int i=0;i<m;i++){
            for (int j = 0; j < n; j++) {
                dist[i][j]=-1;
            }
        }
        Queue<int[]> q=new LinkedList<>();

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(isWater[i][j]==1){
                    dist[i][j]=0;
                    q.add(new int[]{i,j});
                }
            }
        }
        while (!q.isEmpty()){
            int size=q.size();
            for (int i = 0; i < size; i++) {
                int[] tmp=q.poll();
                int a=tmp[0];int b=tmp[1];
                for (int j = 0; j < 4; j++) {
                    int x=a+dx[j];int y=dy[j]+b;
                    if(x>=0&&x<m&&y>=0&&y<n&&dist[x][y]==-1){
                        dist[x][y]=dist[a][b]+1;
                        q.add(new int[]{x,y});
                    }
                }
            }
        }
        return dist;
    }
}